package com.cpg.school.controller;

import com.cpg.school.mybatis.mapper.PrincipalMapper;
import com.cpg.school.mybatis.mapper.StudentMapper;
import com.cpg.school.mybatis.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class ThymeleafController {
    @RequestMapping("/login.do")
    public String login(Model model, String fail) {
        if (fail != null) {
            model.addAttribute("fail", "alert('账号或密码错误!');");
        } else {
            model.addAttribute("fail", "");
        }
        return "login";
    }

    @RequestMapping("/index.do")
    public String index(Model model, HttpSession session) {
        /*session.setAttribute("user_id", "5");
        session.setAttribute("user_type", "2");
        session.setAttribute("login_name", "陈老师");*/
        model.addAttribute("login_name", session.getAttribute("login_name"));
        model.addAttribute("user_type", session.getAttribute("user_type"));
        return "index";
    }

    @RequestMapping("/main.do")
    public String main(Model model) {
        return "main";
    }

    //系管理
    @Autowired
    private PrincipalMapper principalMapper;

    @RequestMapping("/department.do")
    public String department(Model model, String name) {
        int count = principalMapper.department_count(name);
        int pageCount = 1;
        if (count > 15) {
            int ys = count % 15;
            pageCount = (count - count % 15) / 15;
            if (ys > 0) pageCount += 1;
        }
        model.addAttribute("page_count", pageCount);
        return "department";
    }

    @RequestMapping("/department_table.do")
    public String department_table(Model model, String name, int page) {
        List<Map> department_list = principalMapper.department_list(name, page);
        model.addAttribute("department_list", department_list);
        return "department_table";
    }

    @RequestMapping("/department_edit.do")
    public String department_edit(Model model, int id) {
        String department_name = principalMapper.department_detail(id);
        model.addAttribute("department_name", department_name);
        return "department_edit";
    }

    //班级
    @RequestMapping("/grade.do")
    public String grade(Model model) {
        int count = principalMapper.grade_count();
        int pageCount = 1;
        if (count > 15) {
            int ys = count % 15;
            pageCount = (count - count % 15) / 15;
            if (ys > 0) pageCount += 1;
        }
        model.addAttribute("page_count", pageCount);
        return "grade";
    }

    @RequestMapping("/grade_add_view.do")
    public String grade_add(Model model) {
        List<Map> department_select = principalMapper.department_select();
        model.addAttribute("department_select", department_select);
        return "grade_add";
    }

    @RequestMapping("/grade_table.do")
    public String grade_table(Model model, int page) {
        List<Map> grade_list = principalMapper.grade_list(page);
        model.addAttribute("grade_list", grade_list);
        return "grade_table";
    }

    @RequestMapping("/grade_edit.do")
    public String grade_edit(Model model, int id) {
        Map grade_edit = principalMapper.grade_edit(id);
        model.addAttribute("grade_edit", grade_edit);
        return "grade_edit";
    }

    @RequestMapping("/grade_teacher.do")
    public String grade_teacher(Model model, int id) {
        List<Map> type_list = principalMapper.teacher_type_list(0);
        model.addAttribute("type_list", type_list);
        List<Map> teacher_list = principalMapper.teacher_list();
        model.addAttribute("teacher_list", teacher_list);
        List<Map> grade_teacher_list = principalMapper.grade_teacher_list(id, 1);
        ;
        model.addAttribute("grade_teacher_list", grade_teacher_list);
        return "grade_teacher";
    }

    @RequestMapping("/grade_teacher_list_table.do")
    public String grade_teacher_list_table(Model model, int id, int page) {
        List<Map> grade_teacher_list = principalMapper.grade_teacher_list(id, page);
        model.addAttribute("grade_teacher_list", grade_teacher_list);
        return "grade_teacher_list_table";
    }

    //老师管理
    @RequestMapping("/teacher.do")
    public String teacher(Model model) {
        int count = principalMapper.teacher_count();
        int pageCount = 1;
        if (count > 15) {
            int ys = count % 15;
            pageCount = (count - count % 15) / 15;
            if (ys > 0) pageCount += 1;
        }
        model.addAttribute("page_count", pageCount);
        return "teacher";
    }

    @RequestMapping("/teacher_add_view.do")
    public String teacher_add_view(Model model) {
        List<Map> type_list = principalMapper.teacher_type_list(0);
        model.addAttribute("type_list", type_list);
        return "teacher_add_view";
    }

    @RequestMapping("/teacher_table.do")
    public String teacher_table(Model model, int page) {
        List<Map> list = principalMapper.teacher_table_list(page);
        model.addAttribute("list", list);
        return "teacher_table";
    }

    @RequestMapping("/teacher_edit.do")
    public String teacher_edit(Model model, int id) {
        Map data = principalMapper.teacher_edit(id);
        model.addAttribute("data", data);
        return "teacher_edit";
    }

    @RequestMapping("/teacher_type.do")
    public String teacher_type(Model model) {
        int count = principalMapper.teacher_type_count();
        int pageCount = 1;
        if (count > 15) {
            int ys = count % 15;
            pageCount = (count - count % 15) / 15;
            if (ys > 0) pageCount += 1;
        }
        model.addAttribute("page_count", pageCount);
        return "teacher_type";
    }

    @RequestMapping("/teacher_type_table.do")
    public String teacher_type_table(Model model, int page) {
        List<Map> list = principalMapper.teacher_type_list(page);
        model.addAttribute("list", list);
        return "teacher_type_table";
    }

    @RequestMapping("/teacher_type_add_view.do")
    public String teacher_type_add_view(Model model) {
        return "teacher_type_add_view";
    }

    @RequestMapping("/teacher_type_edit_view.do")
    public String teacher_type_edit(Model model, int id) {
        Map data = principalMapper.teacher_type_edit(id);
        model.addAttribute("data", data);
        return "teacher_type_edit_view";
    }

    //学生管理
    @Autowired
    private TeacherMapper teacherMapper;

    @RequestMapping("/student.do")
    public String student(Model model, HttpSession session) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        int count = teacherMapper.student_count(teacher_id);
        int pageCount = 1;
        if (count > 15) {
            int ys = count % 15;
            pageCount = (count - count % 15) / 15;
            if (ys > 0) pageCount += 1;
        }
        model.addAttribute("page_count", pageCount);
        return "student";
    }

    @RequestMapping("/student_list_table.do")
    public String student_list_table(Model model, HttpSession session, int page) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        List<Map> student_list_table = teacherMapper.student_list_table(teacher_id, page);
        model.addAttribute("list", student_list_table);
        return "student_list_table";
    }

    @RequestMapping("/student_add_view.do")
    public String student_add_view(Model model, HttpSession session) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        List<Map> grade_list = teacherMapper.teacher_grade_list(teacher_id);
        model.addAttribute("grade_list", grade_list);
        return "student_add_view";
    }

    @RequestMapping("/student_import_view.do")
    public String student_import_view(Model model, HttpSession session) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        List<Map> grade_list = teacherMapper.teacher_grade_list(teacher_id);
        model.addAttribute("grade_list", grade_list);
        return "student_import_view";
    }

    @RequestMapping("/student_edit_view.do")
    public String student_edit_view(Model model, HttpSession session, int id) {
        Map student = teacherMapper.student_edit_view(id);
        model.addAttribute("student", student);
        return "student_edit_view";
    }

    //作业管理
    @RequestMapping("/question_bank.do")
    public String question_bank(Model model, HttpSession session) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        int count = teacherMapper.question_bank_count(teacher_id);
        int pageCount = 1;
        if (count > 15) {
            int ys = count % 15;
            pageCount = (count - count % 15) / 15;
            if (ys > 0) pageCount += 1;
        }
        model.addAttribute("page_count", pageCount);
        return "question_bank";
    }

    @RequestMapping("/question_bank_list_table.do")
    public String question_bank_list_table(Model model, HttpSession session, int page) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        List<Map> student_list_table = teacherMapper.question_bank_list_table(teacher_id, page);
        model.addAttribute("list", student_list_table);
        return "question_bank_list_table";
    }

    @RequestMapping("/question_bank_add_view.do")
    public String question_bank_add_view() {
        return "question_bank_add_view";
    }

    @RequestMapping("/question_bank_edit_view.do")
    public String question_bank_edit_view(Model model, int id) {
        Map data = teacherMapper.question_bank_edit_view(id);
        model.addAttribute("data", data);
        return "question_bank_edit_view";
    }

    @RequestMapping("/question_subjects.do")
    public String question_subjects(Model model, int id) {
        Map data = teacherMapper.question_bank_edit_view(id);
        List<Map> list = teacherMapper.question_subjects(id);
        model.addAttribute("data", data);
        model.addAttribute("list", list);
        model.addAttribute("id", id);
        return "question_subjects";
    }

    @RequestMapping("/question_subjects_add_view.do")
    public String question_subjects_add_view(Model model, HttpSession session) {
        return "question_subjects_add_view";
    }

    @RequestMapping("/question_subjects_table.do")
    public String question_subjects_table(Model model, int id) {
        List<Map> list = teacherMapper.question_subjects(id);
        model.addAttribute("list", list);
        return "question_subjects_table";
    }

    @RequestMapping("/question_subjects_edit_view.do")
    public String question_subjects_edit_view(Model model, HttpSession session, int id) {
        Map data = teacherMapper.question_subjects_edit_view(id);
        model.addAttribute("data", data);
        return "question_subjects_edit_view";
    }

    @RequestMapping("/question_bank_send.do")
    public String question_bank_send(Model model, HttpSession session) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        int count = teacherMapper.question_bank_send_count(teacher_id);
        int pageCount = 1;
        if (count > 15) {
            int ys = count % 15;
            pageCount = (count - count % 15) / 15;
            if (ys > 0) pageCount += 1;
        }
        model.addAttribute("page_count", pageCount);
        return "question_bank_send";
    }

    @RequestMapping("/question_bank_send_table.do")
    public String question_bank_send_table(Model model, HttpSession session, int page) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        List<Map> list = teacherMapper.question_bank_send_list(teacher_id, page);
        model.addAttribute("list", list);
        return "question_bank_send_table";
    }

    @RequestMapping("/question_bank_send_add_view.do")
    public String question_bank_send_add_view(Model model, HttpSession session) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        List<Map> grade_list = teacherMapper.teacher_grade_list(teacher_id);
        model.addAttribute("grade_list", grade_list);
        List<Map> question_bank_list = teacherMapper.question_bank_list_table(teacher_id, 1);
        model.addAttribute("question_bank_list", question_bank_list);
        return "question_bank_send_add_view";
    }

    @RequestMapping("/question_bank_send_edit_view.do")
    public String question_bank_send_edit_view(Model model, HttpSession session, int id) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        List<Map> grade_list = teacherMapper.teacher_grade_list(teacher_id);
        model.addAttribute("grade_list", grade_list);
        List<Map> question_bank_list = teacherMapper.question_bank_list_table(teacher_id, 1);
        model.addAttribute("question_bank_list", question_bank_list);
        Map data = teacherMapper.question_bank_send_edit_view(id);
        model.addAttribute("data", data);
        return "question_bank_send_edit_view";
    }

    @RequestMapping("/student_answers.do")
    public String student_answers(Model model, HttpSession session) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        List<Map> grade_list = teacherMapper.teacher_grade_list(teacher_id);
        model.addAttribute("grade_list", grade_list);
        return "student_answers";
    }

    @RequestMapping("/select_question_list.do")
    public String select_question_list(Model model, HttpSession session, int id) {
        List<Map> question_list = teacherMapper.select_question_list(id);
        model.addAttribute("question_list", question_list);
        return "select_question_list";
    }

    @RequestMapping("/student_answers_list_table.do")
    public String student_answers_list_table(Model model, HttpSession session, int grade_id, int question_id, int page) {
        List<Map> student_list = teacherMapper.student_answers_list_table(grade_id, question_id, page);
        model.addAttribute("student_list", student_list);
        model.addAttribute("question_id", question_id);
        return "student_answers_list_table";
    }

    @RequestMapping("/student_answers_view.do")
    public String student_answers_view(Model model, HttpSession session, int uid, int qid) {
        List<Map> answers_list = teacherMapper.student_answers_view(uid, qid);
        model.addAttribute("answers_list", answers_list);
        model.addAttribute("uid", uid);
        return "student_answers_view";
    }

    // 学生
    @Autowired
    private StudentMapper studentMapper;

    @RequestMapping("/student_question_bank.do")
    public String student_question_bank(Model model, HttpSession session) {
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        int count = studentMapper.student_question_bank_count(user_id);
        int pageCount = 1;
        if (count > 15) {
            int ys = count % 15;
            pageCount = (count - count % 15) / 15;
            if (ys > 0) pageCount += 1;
        }
        model.addAttribute("page_count", pageCount);
        return "student_question_bank";
    }

    @RequestMapping("/student_question_bank_table.do")
    public String student_question_bank_table(Model model, HttpSession session, int page) {
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        List<Map> table_data = studentMapper.student_question_bank_table(user_id, page);
        model.addAttribute("table_data", table_data);
        return "student_question_bank_table";
    }

    @RequestMapping("/student_answers_view_t.do")
    public String student_answers_view(Model model, HttpSession session, int qid) {
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        List<Map> answers_list = teacherMapper.student_answers_view(user_id, qid);
        model.addAttribute("answers_list", answers_list);
        model.addAttribute("uid", user_id);
        List<Integer> qids = new ArrayList<>();
        for (Map map : answers_list) qids.add(Integer.parseInt(map.get("qsid").toString()));
        model.addAttribute("qids", qids);
        return "student_answers_view_t";
    }

}
